﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace Common
{
    public class ProcessHelper
    {
        public static void KillProcessByName(string processName)
        {
            try
            {
                Process current = Process.GetCurrentProcess();

                Process[] pros = Process.GetProcessesByName(processName);
                if (pros != null)
                {
                    foreach (Process pro in pros)
                    {
                        if (current.Id != pro.Id)
                        {
                            pro.Kill();
                            if (!pro.HasExited)
                            {
                                pro.CloseMainWindow();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //throw ex;
            }
        }
        public static void KillProcessByName(string processName, Process currentPro)
        {
            try
            {
                Process[] pros = Process.GetProcessesByName(processName);
                if (pros != null)
                {
                    foreach (Process pro in pros)
                    {
                        if (currentPro.Id != pro.Id)
                        {
                            pro.Kill();
                            if (!pro.HasExited)
                            {
                                pro.CloseMainWindow();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                KillProcessByName(processName, currentPro);
                //throw ex;
            }
        }
    }
}
